var dataScreenDAO = require('../dao/dataScreenDAO');
var moment = require('moment');

var dataScreenStorage = {
    size:10000,
    pageSize:30,
    data:{
        soldOutRate: 0,
        areaGmv: [],
        orderlist: [],
    },
    mergeOrderList:function(addData){
        let exsitData = this.data.orderlist;
        let addSize = addData.size;
        let compareData = exsitData.slice(0, addSize);
        if(exsitData.length <= 0){
            this.data.orderlist = addData;
        }else{
            addData = addData.filter((item1)=>{
                return !compareData.find(item2 => {
                    return item1.经度 === item2.经度
                    && item1.纬度 === item2.纬度
                    // && item1.下单时间.toString() === item2.下单时间.toString()
                    && item1.金额 === item2.金额
                    && item1.用户名称 === item2.用户名称
                })
            })
            this.data.orderlist = addData.concat(exsitData).slice(0,this.size);
        }
    },
    get:function(size){
        // return this.data.slice(0,size)
    },
    startFetch:function () {
        this.fetchSoldOut(500);
        this.fetchAreaGmv(500);
        this.fetchOrder(500);
    },
    fetchSoldOut(freq){
        setTimeout(function () {
            try {
                dataScreenDAO.getSoldOutRate().then((data)=>{
                    dataScreenStorage.fetchSoldOut(freq);
                    dataScreenStorage.data.soldOutRate = data[0].soldoutrate;
                }).catch(()=>{
                    dataScreenStorage.fetchSoldOut(freq);
                })
            } catch (error) {
                console.warn(error)
            }
        },freq)
    },
    fetchAreaGmv(freq){
        setTimeout(function () {
            try {
                dataScreenDAO.getAreaGmv().then((data)=>{
                    dataScreenStorage.data.areaGmv = data;
                    dataScreenStorage.fetchAreaGmv(freq);
                }).catch(()=>{
                    dataScreenStorage.fetchAreaGmv(freq);
                })
            } catch (error) {
                console.warn(error)
            }
        },freq)
    },
    fetchOrder(freq){
        setTimeout(function () {
            try {
                dataScreenDAO.getOrder(30).then((data)=>{
                    console.log('数据总长', dataScreenStorage.data.orderlist.length)
                    dataScreenStorage.mergeOrderList(data)
                    dataScreenStorage.fetchOrder(freq);
                }).catch(()=>{
                    dataScreenStorage.fetchOrder(freq);
                })
            } catch (error) {
                console.warn(error)
            }
        },freq)
    }
};

module.exports = dataScreenStorage;
